<template>
<!--
  - Copyright (C) 2018-2019
  - All rights reserved, Designed By www.joolun.com
  - 注意：
  - 本软件为www.joolun.com开发研制，项目使用请保留此说明
-->
<view class="flex justify-end">
  <button class="cu-btn radius margin-right shadow-blur service" open-type="contact" @contact="handleContact" v-if="contact">
    <view class="cuIcon-servicefill text-white"></view> 
    <text class="text-white">客服</text>
  </button>
  <button class="cu-btn radius margin-right shadow-blur delete-order" @tap="orderDel" :loading="loading"  :disabled="loading" type="" v-if="orderInfo.status == '5' && orderInfo.isPay == '0'">
    <text class="text-white">删除订单</text>
  </button>
  <button class="cu-btn radius margin-right shadow-blur cancel-order" @tap="orderCancel" :loading="loading"  :disabled="loading" type="" v-if="orderInfo.isPay == '0' && !orderInfo.status">
    <text class="text-white">取消订单</text>
  </button>
  <button class="cu-btn radius margin-right shadow-blur check-logistics" @tap="orderLogistics" :loading="loading"  :disabled="loading" type="" v-if="orderInfo.deliveryWay == '1' && (orderInfo.status == '2' || orderInfo.status == '3' || orderInfo.status == '4')">
    <text class="text-white">查看物流</text>
  </button>
  <button class="cu-btn radius margin-right shadow-blur payment" @tap="unifiedOrder" :loading="loading"  :disabled="loading" type="" v-if="orderInfo.isPay == '0' && !orderInfo.status">
    <text class="text-white">付款</text>
  </button>
  <!-- <button class="cu-btn round line-red margin-right" bindtap="urgeOrder" loading="{{loading}}" 
  disabled="{{loading}}" type="" wx:if="{{orderInfo.status == '1'}}">
    提醒卖家发货
  </button> -->
  <button class="cu-btn radius margin-right shadow-blur confirm-goods" @tap="orderReceive" :loading="loading"  :disabled="loading" type="" v-if="orderInfo.status == '2'">
    <text class="text-white">确认收货</text>
  </button>
  <button class="cu-btn radius margin-right shadow-blur evaluation" @tap="orderAppraise" :loading="loading"  :disabled="loading" type="" v-if="orderInfo.status == '3' && orderInfo.appraisesStatus == '0'">
    <text class="text-white">评价</text>
  </button>
</view>
</template>

<script>
/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 * 注意：
 * 本软件为www.joolun.com开发研制，项目使用请保留此说明
 */
const app = getApp();
export default {data() {
return {
loading: false
};
}, 
props: {
  orderInfo: {
    type: Object,
    default: () => ({})
  },
  callPay: {
    type: Boolean,
    default: false
  },
  contact: {
    type: Boolean,
    default: false
  }
}, 
mounted() {
  let that = this;
  setTimeout(function () {
    if (that.data.callPay) {
      that.unifiedOrder();
    }
  }, 1000);
}, 
methods: {
  orderReceive() {
    let that = this;
    uni.showModal({
      content: '是否确认收货吗？',
      cancelText: '我再想想',
      confirmColor: '#ff0000',

      success(res) {
        if (res.confirm) {
          let id = that.data.orderInfo.id;
          app.globalData.api.orderReceive(id).then(res => {
            that.$emit('orderReceive', {
              detail: res
            });
          });
        }
      }

    });
  },

  orderCancel() {
    let that = this;
    uni.showModal({
      content: '确认取消该订单吗？',
      cancelText: '我再想想',
      confirmColor: '#ff0000',

      success(res) {
        if (res.confirm) {
          let id = that.data.orderInfo.id;
          app.globalData.api.orderCancel(id).then(res => {
            that.$emit('orderCancel', {
              detail: res
            });
          });
        }
      }

    });
  },

  orderDel() {
    let that = this;
    uni.showModal({
      content: '确认删除该订单吗？',
      cancelText: '我再想想',
      confirmColor: '#ff0000',

      success(res) {
        if (res.confirm) {
          let id = that.data.orderInfo.id;
          app.globalData.api.orderDel(id).then(res => {
            that.$emit('orderDel', {
              detail: res
            });
          });
        }
      }

    });
  },

  unifiedOrder() {
    this.setData({
      loading: true
    });
    var that = this;
    let orderInfo = this.data.orderInfo;
    app.globalData.api.unifiedOrder({
      id: orderInfo.id
    }).then(res => {
      this.setData({
        loading: false
      });

      if (orderInfo.paymentPrice <= 0) {
        //0元付款
        that.$emit('unifiedOrder', {
          detail: res
        });
      } else {
        let payData = res.data;
        uni.requestPayment({
          'timeStamp': payData.timeStamp,
          'nonceStr': payData.nonceStr,
          'package': payData.packageValue,
          'signType': payData.signType,
          'paySign': payData.paySign,
          'success': function (res) {
            that.$emit('unifiedOrder', {
              detail: res
            });
          },
          'fail': function (res) {},
          'complete': function (res) {
            console.log(res);
          }
        });
      }
    }).catch(() => {
      this.setData({
        loading: false
      });
    });
  },

  urgeOrder() {
    uni.showToast({
      title: '已提醒卖家发货',
      icon: 'success',
      duration: 2000
    });
  },

  orderLogistics() {
    uni.navigateTo({
      url: '/pages/order/order-logistics/index?id=' + this.data.orderInfo.orderLogistics.id
    });
  },

  orderAppraise() {
    uni.navigateTo({
      url: '/pages/appraises/form/index?orderId=' + this.data.orderInfo.id
    });
  },

  handleContact() {
    console.log("占位：函数 handleContact 未声明");
  }
}};
</script>
<style >

.service{
  background-color: #13b229 !important; 
  width:160rpx;
}
.delete-order{
  background-color: #2d2d2f !important; 
  width:200rpx;
}
.cancel-order{
  background-color: #2d2d2f !important; 
  width:200rpx;
}
.check-logistics{
  background-color: #2d2d2f !important; 
  width:200rpx;
}
.payment{
  background-color: #2967ff !important; 
  width:200rpx;
}
.confirm-goods{
  background-color: #2967ff !important; 
  width:200rpx;
}
.evaluation{
  background-color: #2d2d2f !important; 
  width:200rpx;
}
</style>